package com.demo01.demo.controller;

import com.demo01.demo.Repository.MilkteaRepository;
import com.demo01.demo.entity.Milktea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/test")
public class MilketeaHandler {
    @Autowired
    private MilkteaRepository milkteaRepository;

    @GetMapping("/goodsfindAll/{page}/{size}")
    public Page<Milktea> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return milkteaRepository.findAll(pageable);
    }

    @PostMapping("/goodssave")
    public String save(@RequestBody Milktea milktea) {
        if (milktea.getType().equals("1"))
            milktea.setTypename("果茶家族");
        else if (milktea.getType().equals("2"))
            milktea.setTypename("茗茶/牛乳");
        else if (milktea.getType().equals("3"))
            milktea.setTypename("波波家族");
        else if (milktea.getType().equals("4"))
            milktea.setTypename("醇香咖啡");
        Milktea result = milkteaRepository.save(milktea);
        if (result != null) {
            return "success";
        } else {
            return "error";
        }
    }

    @GetMapping("/goodssort/{page}/{size}")
    public Page<Milktea> Sort(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Sort sort = Sort.by(Sort.Direction.ASC, "type");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return milkteaRepository.findAll(pageable);
    }

    @GetMapping("/goodssortall")
    public List<Milktea> Sortall() {
        Sort sort = Sort.by(Sort.Direction.ASC, "type");
        return milkteaRepository.findAll(sort);
    }

    @PostMapping("/goodssearch/{page}/{size}")
    public Page<Milktea> search(@RequestBody Milktea milktea, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        String[] sql = {"", "", "", "", "", ""};
        if (milktea.getId() != null) {
            sql[0] = milktea.getId();
        }
        if (milktea.getName() != null) {
            sql[1] = milktea.getName();
        }
        if (milktea.getDes() != null) {
            sql[2] = milktea.getDes();
        }
        if (milktea.getPrice() != null) {
            sql[3] = milktea.getPrice().toString();
        }
        if (milktea.getType() != null) {
            sql[4] = milktea.getType();
        }
        if (milktea.getTypename() != null) {
            sql[5] = milktea.getTypename();
        }
        return milkteaRepository.find(sql[0], '%' + sql[1] + '%', sql[2], sql[3], sql[4], sql[5], pageable);
    }

    @PostMapping("/goodssearchall")
    public List<Milktea> searchall(@RequestBody Milktea milktea) {
        String[] sql = {"", "", "", "", "", ""};
        if (milktea.getId() != null) {
            sql[0] = milktea.getId();
        }
        if (milktea.getName() != null) {
            sql[1] = milktea.getName();
        }
        if (milktea.getDes() != null) {
            sql[2] = milktea.getDes();
        }
        if (milktea.getPrice() != null) {
            sql[3] = milktea.getPrice().toString();
        }
        if (milktea.getType() != null) {
            sql[4] = milktea.getType();
        }
        if (milktea.getTypename() != null) {
            sql[5] = milktea.getTypename();
        }
        return milkteaRepository.searchall(sql[0], '%' + sql[1] + '%', sql[2], sql[3], sql[4], sql[5]);
    }

    @GetMapping("/goodsfindById/{goods_id}")
    public Milktea findById(@PathVariable("goods_id") String id) {
        return milkteaRepository.findById(id).get();
    }

    @PutMapping("/goodsupdate")
    public String update(@RequestBody Milktea milktea) {
        Milktea result = milkteaRepository.save(milktea);
        if (result != null) {
            return "success";
        } else {
            return "error";
        }
    }

    @DeleteMapping("/goodsdeleteById/{goods_id}")
    public void deleteById(@PathVariable("goods_id") String id) {
        milkteaRepository.deleteById(id);
    }

    @GetMapping("/goodsall")
    public List<Milktea> findAll(){
        return milkteaRepository.findAll();
    }
}
